package com.server.chart;

import com.dao.chart.ChartsConfigMapper;
import com.dao.dataSource.DataConnectorMapper;
import com.dao.chart.ShowChartsMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.model.po.Databases;
import com.model.pojo.RepCode;
import com.model.pojo.RepEntity;
import com.model.vo.configVo.ShowDataChartInfo;
import com.model.vo.dataVo.ColumnValuesInfo;
import com.config.BasesSource.DynamicDataSourceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class GetChartsDataUtilService {
    @Autowired
    ChartsConfigMapper chartsConfigMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    DataConnectorMapper dataConnectorMapper;
    @Autowired
    ChartsUtilService chartsUtilService;
    @Autowired
    ShowChartsMapper showChartsMapper;
    @Autowired
    GetChartsDataUtilService getChartsDataUtilService;

    /*
    通过图表ID获取数据库连接ID
     */
    public int getBaseId(int id){

        //取数据源ID
        String dataConfig = chartsConfigMapper.getBaseConifg(id);

        //取数据连接名的数据库名
        Databases databases = new Databases();
        try {
            databases = objectMapper.readValue(dataConfig, Databases.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int baseId = databases.getId();

        return baseId;
    }

    /*
    通过数据源ID获取数据库连接ID
     */
    public int getBaseIdByConnect(int id){

        //取数据源ID
        String dataConfig = dataConnectorMapper.getDbConfig(id);

        //取数据连接名的数据库名
        Databases databases = new Databases();
        try {
            databases = objectMapper.readValue(dataConfig, Databases.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int baseId = databases.getId();

        return baseId;
    }

    //取数据源Id
    public int getConnectId(int id){

        //取数据源ID
        int ConnectId = chartsConfigMapper.getBaseId(id);

        return ConnectId;
    }

    /*
    查看图表列数据信息
     */
    public RepEntity getChartsData(String token, ShowDataChartInfo showDataChartInfo){
        int chartsId = showDataChartInfo.getChartId();
        //取表名;
        String tableName = chartsUtilService.getSqlStr(token, chartsId, 0);
        if ("".equals(tableName)){
            return new RepEntity(RepCode.NoAuthority);
        }

        int baseId = getChartsDataUtilService.getBaseId(chartsId);
        System.out.println("切换数据库"+baseId);

        ColumnValuesInfo columnValuesInfo = new ColumnValuesInfo();

        String columnConfig = chartsConfigMapper.getConfigByChartId(chartsId);
        String sql = chartsUtilService.getSqlStr(token, chartsId, 0);
        if ("".equals(sql)) {
            return new RepEntity(RepCode.NoAuthority);
        }
        List<Map<String, Object>> values = new ArrayList<>();
        try {
            if (!DynamicDataSourceContextHolder.isContainsDataSource(String.valueOf(baseId))) {
                //joinPoint.getSignature() ：获取连接点的方法签名对象
                System.out.println("数据源 " + baseId + " 不存在使用默认的数据源 -> ");
                return new RepEntity(RepCode.DataSourceNull);
            } else {
                System.out.println("使用数据源：" + baseId);
                DynamicDataSourceContextHolder.setDataSourceType(String.valueOf(baseId));
            }
            values = chartsConfigMapper.getChartsData(sql, showDataChartInfo.getTestPage().enablePaging());
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(values);
            columnValuesInfo.setValues(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("异常");
            return new RepEntity(RepCode.erro);
        }finally {
            System.out.println("关闭数据源");
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        columnValuesInfo.setColumnConfig(columnConfig);
        return new RepEntity(RepCode.success, columnValuesInfo);
    }
}
